package org.me;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import org.jivesoftware.smack.util.StringUtils;
import org.me.adapter.Adapter_Conversation;
import org.me.utils.Koneksi;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Conversation extends Activity implements OnItemClickListener {

	ListView conList;
	Adapter_Conversation adapter;
	String user_name;
	ArrayList<String> conversation;
	ArrayList<String> con_from;
	ArrayList<String> con_to;
	ArrayList<String> con_mess;
	ArrayList<Integer> index_from;
	ArrayList<Integer> index_to;
	private ArrayList<String> con_temp;
	static Handler mHandler = new Handler();
	static Runnable con_run;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversation);
		
		user_name = StringUtils.parseName(Koneksi.getInstance().getConnection().getUser());
		
		Typeface font = Typeface.createFromAsset(getAssets(), "fonts/tt0142m_.ttf");
		
		TextView conname = (TextView) findViewById(R.id.con_name);
        conname.setTypeface(font);
        conname.setText(user_name);
        TextView constatus = (TextView) findViewById(R.id.con_status);
        constatus.setTypeface(font);
        
		conList = (ListView) this.findViewById(R.id.listConversation);
		
		con_from = Koneksi.getFrom();
		con_to = Koneksi.getTo();
		con_mess = Koneksi.getMess();
		index_from = new ArrayList<Integer>();
		index_to = new ArrayList<Integer>();
		conversation = new ArrayList<String>();
 
		
        con_run = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i=0; i<con_from.size(); i++) {
					if (con_from.get(i).equalsIgnoreCase(user_name)) {
						index_from.add(i);
						conversation.add(con_to.get(i));
					}
				}
//				System.out.println("##########|index_from= "+index_from);
				
				for (int i=0; i<con_to.size(); i++) {
					if (con_to.get(i).equalsIgnoreCase(user_name)) {
						index_to.add(i);
						conversation.add(con_from.get(i));
					}
				}
//				System.out.println("##########|index_to= "+index_to);
				
				Set<String> st = new LinkedHashSet<String>(conversation);
		        con_temp = new ArrayList<String>(st);
		        Collections.sort(con_temp);
		        
//				System.out.println("##########|con_temp= "+con_temp);
				adapter = new Adapter_Conversation(getBaseContext(), con_temp);
				conList.setAdapter(adapter);
				mHandler.postDelayed(this, 2000);
			}
		};
		mHandler.post(con_run);
		
		conList.setOnItemClickListener(this);
		registerForContextMenu(conList);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		String click = con_temp.get(position);
		
//		ArrayList<String> groups = Koneksi.getInstance().getAllGroup();
//        Set<String> s = new LinkedHashSet<String>(groups);
//        ArrayList<String> temp = new ArrayList<String>(s);
//        Collections.sort(temp);
//		ArrayList<ArrayList<String>> exContact = new ArrayList<ArrayList<String>>();
//        
//		for (int i=0; i<temp.size(); i++) {
//			exContact.add(new ArrayList<String>());
//		}
//		for (int i=0; i<temp.size(); i++) {
//			exContact.get(i).addAll(Koneksi.getInstance().getContactOneGroup(temp.get(i)));
//		}
//		
//		for (int i=0; i<exContact.size(); i++) {
//			for (int j=0; j<exContact.get(i).size(); j++) {
//				if (click.equalsIgnoreCase(exContact.get(i).get(j))) {
//					Koneksi.getInstance().setChildPosition(j);
//			        Koneksi.getInstance().setGroupPosition(i);
//				}
//			}
//		}
//		
		
		Intent myIntent = new Intent(view.getContext(), Client.class);
		myIntent.putExtra("userChat", click+"@handry-pc");
		myIntent.putExtra("roster", true);
		startActivity(myIntent);
	}
	
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(Intent.ACTION_MAIN);
	    intent.addCategory(Intent.CATEGORY_HOME);
	    startActivity(intent);
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
	    ContextMenuInfo menuInfo) {
	  if (v.getId()==R.id.listConversation) {
	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle(con_temp.get(info.position));
	    String[] menuItems = {"delete"};
	    for (int i = 0; i<menuItems.length; i++) {
	      menu.add(Menu.NONE, i, i, menuItems[i]);
	    }
	  }
	}
	
	public boolean onContextItemSelected(MenuItem item) {
	  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	  int menuItemIndex = item.getItemId();
	  String[] menuItems = {"delete"};
	  String menuItemName = menuItems[menuItemIndex];
	  String listItemName = con_temp.get(info.position);

	  Toast.makeText(this, String.format("Selected %s for item %s", menuItemName, listItemName), Toast.LENGTH_LONG).show();
//	  TextView text = (TextView)findViewById(R.id.footer);
//	  text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
	  return true;
	}
	
}
